package com.cmxh.cloundnote.Controller;

import com.aliyun.oss.OSSClient;
import com.cmxh.cloundnote.pojo.Upwd;
import com.cmxh.cloundnote.pojo.User;
import com.cmxh.cloundnote.service.UserService;
import com.cmxh.cloundnote.util.Code;
import com.cmxh.cloundnote.util.Result;
import com.cmxh.cloundnote.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OSSUtil ossUtil;
    @RequestMapping("/zhuce")
    public Result save(@RequestBody User xx){
        int i = userService.saveUser(xx);
        if (i>0){
            return new Result(Code.SAVE_OK,"注册成功",null);
        }else {
            return new Result(Code.GET_ERR,"注册失败",null);
        }
    }
    @RequestMapping("/denglu")
    public Result login(@RequestBody User xx){
        return userService.login(xx);

    }
    @RequestMapping("/update")
    public Result updatePassword(@RequestBody Upwd xx){
        System.out.println(xx);
        return userService.updatePassword(xx);
    }
    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody User xx){
        System.out.println(xx);
        return userService.updateUser(xx);
    }
    @RequestMapping("/tx")
    public Result xx(MultipartFile file) throws IOException {
        System.out.println("后端" + file);
        String originalFilename = file.getOriginalFilename();
        String newname = UUID.randomUUID().toString().substring(24) + "-" + originalFilename;
        OSSClient xx = new OSSClient(ossUtil.getYuming(), ossUtil.getId(), ossUtil.getSecret());
        xx.putObject(ossUtil.getTong(), newname, file.getInputStream());
        String url1 = ossUtil.getYuming();
        String[] split = url1.split("//");
        String url = split[0] + "//" + ossUtil.getTong() + "." + split[1] + "/" + newname;
        return new Result(Code.SAVE_OK, url, "上传成功");
    }
    @RequestMapping("/tx1")
    public Result tx(@RequestBody User user) {
        int tx = userService.tx(user);
        System.out.println(user);
        if (tx > 0) {
            return new Result(Code.SAVE_OK, "上传成功");
        } else {
            return new Result(Code.GET_ERR, "上传失败");
            }
    }
}
